/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.java.Mail;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.mail.MessagingException;

/**
 *
 * @author Zecons
 */
public class ReadTime {
    
    private String filetimes,fileemails,attachname,frommail,password;
    String[] attachFiles = new String[1];
    SendingMail sm = new SendingMail();
    DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmm");
    Date d = new Date();
    
    
    public void TimeRead() {
        try {
        BufferedReader br = new BufferedReader(new FileReader("C:/javamail/downloadtime.txt"));
        BufferedReader br1 = new BufferedReader(new FileReader("C:/javamail/mails.txt"));
        BufferedReader br2 = new BufferedReader(new FileReader("C:/javamail/autodownload.txt"));
        BufferedReader br3 = new BufferedReader(new FileReader("C:/javamail/frommail.txt"));
        BufferedReader br4 = new BufferedReader(new FileReader("C:/javamail/password.txt"));
        BufferedReader br5 = new BufferedReader(new FileReader("C:/javamail/host.txt"));
        
           String host;// = "smtp.gmail.com";
            String port = "25";
           // String mailFrom = "prajja1990@gmail.com";
           // String password = "amma2468524685";

            // message info

            String subject = "Error Log File";           
            String message = "Hello";
            
            // attachments
           
           // attachFiles[0] = "D:/LIKITHA/error.txt";
            
            // usermail
            if((frommail=br3.readLine())!=null)
            {
                frommail = frommail;
            }
            // password
             if((password=br4.readLine())!=null)
            {
                password = password;
            }
              if((host=br5.readLine())!=null)
            {
                host = host;
            }
            
            if((attachname=br2.readLine())!=null)
            {
                attachFiles[0]=attachname;
            }
            
            File oldfile = new File(attachFiles[0]);
            String path = oldfile.getParent();
            String a = dateFormat.format(d);

            Calendar cal = Calendar.getInstance();
            cal.getTime();
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            String systime = sdf.format(cal.getTime());
            System.out.print(systime);
            
           
            while ((filetimes = br.readLine()) != null) {
                //System.out.println(sCurrentLine);                
                if (filetimes.equals(systime)) 
                {
                    System.out.print("Likitha");
                      Thread.sleep(60000);
                            while ((fileemails = br1.readLine()) != null) 
                            {                                
                                System.out.println(fileemails);
                                sm.Sendmail(host, port, frommail, password, fileemails, subject, message, attachFiles);
                                System.out.println("sent");
                            }                   
                      // Thread.sleep(60000);
                     File newfile = new File(path+"/"+a+".txt");
                     
                      if (oldfile.renameTo(newfile)) {
                System.out.println("Rename succesful");
            } else {
                System.out.println("Rename failed");
            }
                                     }
           
                
            }
        } catch (Exception e) {
        }
    }
        
}
